// index.ts
// 获取应用实例
const app = getApp<IAppOption>()
Page({
  data: {
    showDialog: false as boolean,
    type: 0 as number,//1-锻炼，2-旅游， 0-默认
    timer: null as number | null,
    containerHeight: 0 as number, //容器的高度 
    startY: 0 as number,
    endY: 0 as number,
    isTouch: false as boolean,
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs',
    })
  },
  onChooseAvatar(e: any) {
    const { avatarUrl } = e.detail
    this.setData({
      avatarUrl,
    })
  },
  toActicity(): void {
    console.log("toActicity执行")
    wx.navigateTo({
      url: "/pages/activity/activity"
    })
  },
  onLoad(): void {
    const request = require("../../api/move")
    // 获取部位项，并存储在globalData中
    request.getNavItem().then((res: Response) => {
      if (!res.code) {
        app.globalData.exerciseArea = res.data
      }
    })
    const query = wx.createSelectorQuery();
    query.select(".home").boundingClientRect();
    query.exec((res) => {
      if (res && res[0]) {
        const containerHeight = res[0].height;
        this.setData({
          containerHeight
        });
      } else {
        console.error('无法获取容器高度');
      }
    });
  },
  chooseType(): void {
    this.setData({
      type: 1
    })
    let timer = setTimeout(() => {
      if (timer) {
        clearTimeout(timer)
      }
      this.setData({
        timer,
        showDialog: true,
      })
    }, 1000)
  },
  closeType(): void {
    this.setData({
      type: 0,
      showDialog: false
    })
  },
  // 根据模板创建
  toTemplate(): void {
    wx.navigateTo({
      url: "/pages/template/template"
    })
  },
  onShow(): void {
    this.setData({
      showDialog: false,
      type: 0
    })
  },
  toplan(): void {
    this.setData({
      type: 2
    })
    let timer = setTimeout(() => {
      if (timer) {
        clearTimeout(timer)
      }
      this.setData({
        timer,
      })
    }, 1000)
  },
  touchStartExercise(e: any): void {
    this.setData({
      startY: e.touches[0].clientY,
      isTouch: true
    })
  },
  touchEndExercise(e: any): void {
    if(!this.data.isTouch) {
      return
    }
    this.setData({
      isTouch: false
    })
    const deltaY = this.data.endY - this.data.startY;
    const threshold = this.data.containerHeight / 5;
    console.log(deltaY, threshold)
    console.log(e.currentTarget.dataset.type)
    if (e.currentTarget.dataset.type == "exercise") {
      if (Math.abs(deltaY) > threshold) {
        // 滑动距离超过容器高度的三分之一
        if (deltaY > 0 && this.data.type == 0) {
          this.chooseType();
        } else if (deltaY < 0 && this.data.type == 0) {
          this.setData({
            type: 0
          })
        }
      }
    }
    else {
      if (Math.abs(deltaY) > threshold) {
        // 滑动距离超过容器高度的三分之一
        if (deltaY > 0 && this.data.type == 2) {
          this.setData({
            type: 0
          });
        } else if (deltaY < 0 && this.data.type == 0) {
          this.toplan();
        }
      }
    }
  },
  touchMoveExercise(e: any) : void{
    const currentY = e.touches[0].clientY;
    this.setData({
      endY: currentY
    });
  },
  chooseMap(): void {
    wx.navigateTo({
      url: "/pages/map/map"
    })
  }
})
